layui.config({
	base : "js/"
}).use(['form','layer','jquery','laypage'],function(){
	var form = layui.form(),
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage,
		$ = layui.jquery;
	$('.audit_btn').click(function(){
		chkeckComment();
	});
	//操作
	$("body").on("click",".audit_btn",function(){  //编辑
		chkeckComment($(this));
	})
	function chkeckComment(t){
		var id =$(t).attr('data-id'), p = id?"?id="+id:'';
		$.get(ctx+"/comment/chckedAllOrId"+p, function(data){
			if(!data.code&&data.code==0){
				 layer.msg('完成', {icon: 1,time: 1500}); 
			}else{
				 layer.msg('请求失败，请刷新页面重试', {icon: 5,time: 1500}); 
			}
		})
	}
	var newsData = '';
	function initPage(){
		//加载页面数据
		$.get(ctx+"/comment/getCommnetList.json?"+new Date().getTime(), function(data){
			if(!data.code&&data.code==0){
				newsList(data.data);
			}else{
				 layer.msg('请求失败，请刷新页面重试', {icon: 5,time: 1500}); 
			}
		})
	}
	initPage();
	//全选
	form.on('checkbox(allChoose)', function(data){
		var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"])');
		child.each(function(index, item){
			item.checked = data.elem.checked;
		});
		form.render('checkbox');
	});
	function newsList(that){
		//渲染数据
		var renderDate = function (currData){
			var dataHtml = '';
			if(currData.length != 0){
				for(var i=0;i<currData.length;i++){
					let comment= ((currData[i].comment).length>30?(currData[i].comment).substring(0,30)+'...':currData[i].comment);
					dataHtml += '<tr>'
			    	+'<td><input type="checkbox" name="checked" lay-skin="primary" lay-filter="choose"></td>'
			    	+'<td align="left">'+currData[i].commentId+'</td>'
			    	+'<td>'+currData[i].commentUserId+'</td>'
			    	+'<td>'+currData[i].contentId+'</td>'
			    	+'<td>'+currData[i].createTime+'</td>'
			    	+'<td>'+currData[i].isRecommend+'</td>'
			    	+'<td>'+(currData[i].isChecked==2?'是':'否')+'</td>'
					+'<td title='+currData[i].comment+'>'+comment+'</td>';
			    	dataHtml += '<td><a href="JavaScript:void(0);" class="layui-btn layui-btn-mini audit_btn" data-id="'+currData[i].commentId+'"><i class="layui-icon">&#xe618;</i>审核</a>'
			        +'</td>'
			    	+'</tr>';
				}
			}else{
				dataHtml = '<tr><td colspan="8">暂无数据</td></tr>';
			}
		    return dataHtml;
		}
		//分页
		if(that)newsData = that.list;
		laypage({
			cont : "page",
			pages : that.pages,
			jump : function(obj,first){
				 if(!first){
					 $.get(ctx+"/comment/getCommnetList.json?pageNum="+obj.curr, function(data){
							if(!data.code&&data.code==0){
								$(".news_content").html(renderDate(data.data.list));
								$('.news_list thead input[type="checkbox"]').prop("checked",false);
						    	form.render();
							}else{
								 layer.msg('请求失败，请刷新页面重试', {icon: 5,time: 1500}); 
							}
						}) 
				 }else{
						$(".news_content").html(renderDate(newsData));
						$('.news_list thead input[type="checkbox"]').prop("checked",false);
						form.render();
				 }
			}
		})
	}
})
